<?xml version="1.0" ?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:lzc="http://schemas.icitadel.com/Lazarus/Compiled.xsd" version="1.0">

  <xsl:output method="text" />

  <xsl:include href="../../common.xsl" />
  <xsl:include href="./util.xsl" />

  <xsl:template match="lzc:project">/*
* Copyright 2012 Google Inc. All Rights Reserved.
*
* __author__ = joemu@google.com (Joe Allan Muharsky)
*
* Creates the tables and relationships for the <xsl:value-of select="@name" /> database.
*
*/

/* Drop the database and re-create it, if it already exists. */
DROP DATABASE IF EXISTS <xsl:value-of select="lzc:datasource/@schema" />;
CREATE DATABASE <xsl:value-of select="lzc:datasource/@schema" />;
USE <xsl:value-of select="lzc:datasource/@schema" />;

<xsl:for-each
      select="lzc:tables/lzc:table[ count( lzc:fields/lzc:field ) &gt; 0 ]">
      <xsl:if test="lzc:description">
        <xsl:text># </xsl:text>
        <xsl:call-template name="comment-description">
          <xsl:with-param name="word" select="lzc:description" />
        </xsl:call-template>
      </xsl:if>
CREATE TABLE <xsl:value-of select="@alias" />
      <xsl:text>
(</xsl:text>
      <xsl:for-each select="lzc:fields/lzc:field">
        <xsl:text>
   </xsl:text>
        <xsl:value-of select="@dbname" />
        <xsl:value-of select="$space" />
        <xsl:call-template name="getSqlTableType">
          <xsl:with-param name="pFieldType" select="@type" />
        </xsl:call-template>
        <xsl:if test="( string-length( @size ) &gt; 0 ) and ( @size &gt; 0 )">
          <xsl:text>(</xsl:text>
          <xsl:value-of select="@size" />
          <xsl:if
            test="( string-length( @scale ) &gt; 0 ) and ( @scale &gt; 0 )">
            <xsl:text>, </xsl:text>
            <xsl:value-of select="@scale" />
          </xsl:if>
          <xsl:text>)</xsl:text>
        </xsl:if>
        <xsl:if test="@required = 'True'"> NOT NULL</xsl:if>
        <xsl:if test="lzc:indexes/lzc:index[@type=$INDEXTYPE_PRIMARY]"> PRIMARY KEY</xsl:if>
        <xsl:if test="position() &lt; count( ../lzc:field )">
          <xsl:text>,</xsl:text>
        </xsl:if>
      </xsl:for-each>
);

</xsl:for-each>

    <!-- Create the defaults -->
    <xsl:text>

# Defaults</xsl:text>
    <xsl:for-each
      select="lzc:tables/lzc:table[ count( lzc:fields/lzc:field ) &gt; 0 ]">
      <xsl:variable name="DEFAULT_FIELDS" 
        select="lzc:fields/lzc:field[ (@default or @autogenerate = 'True') and @type != 'Identity' ]" />
      <xsl:if test="count( $DEFAULT_FIELDS ) &gt; 0">
        <xsl:variable name="TABLE_NAME">
          <xsl:value-of select="@alias" />
        </xsl:variable>
        <xsl:text>
ALTER TABLE </xsl:text>
        <xsl:value-of select="$TABLE_NAME" />
        <xsl:for-each select="$DEFAULT_FIELDS">
        <xsl:variable name="DEFAULT">
          <xsl:call-template name="default-value">
            <xsl:with-param name="field" select="." />
          </xsl:call-template>
        </xsl:variable>
        <xsl:if test="position() &gt; 1">,</xsl:if>
  ALTER <xsl:value-of select="@name" /> SET DEFAULT <xsl:value-of select="$DEFAULT" />
        </xsl:for-each>
        <xsl:text>;
</xsl:text>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="default-value">
    <xsl:param name="field" />
    <xsl:choose>
      <xsl:when test="$field/@autogenerate = 'True'">
        <xsl:choose>
          <xsl:when test="$field/@type = 'DateTime'">
            <xsl:text>CURRENT_TIMESTAMP</xsl:text>
          </xsl:when>
          <xsl:otherwise>
            <xsl:text>UNDEFINED</xsl:text>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <xsl:choose>
          <xsl:when test="$field/@type = 'Varchar'">
            <xsl:text>'</xsl:text>
            <xsl:value-of select="$field/@default" />
            <xsl:text>'</xsl:text>
          </xsl:when>
          <xsl:otherwise>
            <xsl:text>UNDEFINED</xsl:text>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>